﻿using System;
using System.IO;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRevitColumn.ColumnModule.DataDefine;

namespace YJKRevitColumn.ColumnModule
{
	public class ColumnFamilyDesiger
	{
		public ColumnFamilyDesiger(ExternalCommandData cmdData, ColumnFamily familyInfos, ElementId materialId)
		{
			this.m_Revit = cmdData;
			this.m_RevitApp = this.m_Revit.Application.Application;
			this.m_FamilyInfos = familyInfos;
			this.m_MaterialId = materialId;
		}

		public Result Design(ref string familyFilePath, ref string message)
		{
			Result result = this.OpenAndActiveFamilyTemplateDocument(ref message);
			if (result != Autodesk.Revit.UI.Result.Succeeded)
			{
				return result;
			}
			Transaction transaction = new Transaction(this.m_FamilyDocument);
			transaction.Start("create family");
			this.m_FamilyCreator = new IrregularColumnFamilyCreator(this.m_Revit, this.m_FamilyInfos, this.m_MaterialId, this.m_FamilyDocument);
			result = this.Create(ref message);
			if (result != Autodesk.Revit.UI.Result.Succeeded)
			{
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
				this.DocumentClose();
				return result;
			}
			transaction.Commit();
			result = this.SaveFamilyFile(ref familyFilePath, ref message);
			if (result != Autodesk.Revit.UI.Result.Succeeded)
			{
				this.DocumentClose();
				return result;
			}
			return result;
		}

		private Result Create(ref string message)
		{
			Result result = this.m_FamilyCreator.AddReferencePlanes(ref message);
			if (result != Autodesk.Revit.UI.Result.Succeeded)
			{
				return result;
			}
			result = this.m_FamilyCreator.CreateExtrusion(ref message);
			if (result != Autodesk.Revit.UI.Result.Succeeded)
			{
				return result;
			}
			result = this.m_FamilyCreator.AddAlignments(ref message);
			if (result != Autodesk.Revit.UI.Result.Succeeded)
			{
				return result;
			}
			result = this.m_FamilyCreator.AddParameters(ref message);
			if (result != Autodesk.Revit.UI.Result.Succeeded)
			{
				return result;
			}
			result = this.m_FamilyCreator.AddDimensions(ref message);
			if (result != Autodesk.Revit.UI.Result.Succeeded)
			{
				return result;
			}
			result = this.m_FamilyCreator.AddSymbols(ref message);
			if (result != Autodesk.Revit.UI.Result.Succeeded)
			{
				return result;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private Result OpenAndActiveFamilyTemplateDocument(ref string message)
		{
			string text = AssistFunc.GetAppRootPath();
			string versionNumber = this.m_Revit.Application.Application.VersionNumber;
			text = text + "\\Templates\\" + versionNumber + "\\公制结构柱.rft";
			if (!File.Exists(text))
			{
				throw new Exception("Unsupported version");
			}
			this.m_FamilyDocument = this.m_RevitApp.NewFamilyDocument(text);
			if (this.m_FamilyDocument == null)
			{
				message = "族模板目录缺少‘公制结构柱.rft’文件，无法创建族文档.";
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private Result SaveFamilyFile(ref string familyFilePath, ref string message)
		{
			string tempPath = Path.GetTempPath();
			familyFilePath = tempPath + this.m_FamilyInfos.Name + ".rfa";
			if (File.Exists(familyFilePath))
			{
				File.Delete(familyFilePath);
			}
			RevitVersionFuncs.SaveAs(this.m_FamilyDocument, familyFilePath);
			this.m_FamilyDocument.Close(true);
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void DocumentClose()
		{
			this.m_FamilyDocument.Close(false);
		}

		private ExternalCommandData m_Revit;

		private Document m_FamilyDocument;

		private ColumnFamily m_FamilyInfos;

		private ElementId m_MaterialId;

		private ColumnFamilyCreator m_FamilyCreator;

		private Application m_RevitApp;
	}
}
